package com.summer.peony.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.AjaxResult.Type;
import com.summer.peony.system.pojo.SysUser;
import com.summer.peony.system.service.SysUserService;

/**
 * @ClassName: SysUserController
 * @Description: TODO(用户管理控制层)
 * @author: TaoGe
 * @date: 2020年6月29日 下午2:05:09
 * @version: v0.0.1
 */
@RestController
@RequestMapping("/system")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 新增用户
	 * 
	 * @param sysUser
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月29日 下午4:16:45
	 * @throws:
	 */
	@PostMapping("/addSysUser")
	public AjaxResult addSysUser(@RequestBody SysUser sysUser) {
		return sysUserService.insert(sysUser);
	}

	/**
	 * 修改用户
	 * 
	 * @param sysUser
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月29日 下午5:18:16
	 * @throws:
	 */
	@PutMapping("/updateUser")
	public AjaxResult updSysUser(@RequestBody SysUser sysUser) {
		return sysUserService.updateByPrimaryKey(sysUser);
	}

	/**
	 * 删除用户
	 * 
	 * @param userId
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月29日 下午5:18:36
	 * @throws:
	 */
	@DeleteMapping("/delSysUser/{userId}")
	public AjaxResult delSysUser(@PathVariable Integer userId) {
		int key = sysUserService.deleteByPrimaryKey(userId);
		AjaxResult ajaxResult = new AjaxResult();
		if (key == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "删除成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 900005);
		ajaxResult.put("msg", "删除用户失败");
		return ajaxResult;
	}

	/**
	 * 分页查询用户列表
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月29日 下午5:18:46
	 * @throws:
	 */
	@GetMapping("/selectAllSysUser")
	public AjaxResult selectAllSysUser(String username, String phone, Integer pageNum, Integer pageSize) {
		PageInfo<SysUser> pageInfo = sysUserService.selectAllUsers(username, phone, pageNum, pageSize);
		return new AjaxResult(Type.SUCCESS, "查询成功", pageInfo);
	}

	/**
	 * 查询单个用户
	 * 
	 * @param userId
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月30日 下午2:50:01
	 * @throws:
	 */
	@GetMapping("/selectUser/{userId}")
	public AjaxResult selectUser(@PathVariable Integer userId) {
		SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
		if (sysUser != null) {
			return new AjaxResult(Type.SUCCESS, "查询成功", sysUser);
		}
		return new AjaxResult(Type.ERROR, "查询失败");
	}

	@GetMapping("/userPwd")
	public AjaxResult userPwd(Integer userId) {
		Integer userPwd = sysUserService.userPwd(userId);
		if (userPwd == 1) {
			return new AjaxResult(Type.SUCCESS, "重置密码成功");
		}
		return new AjaxResult(Type.ERROR, "重置密码失败");
	}
}
